var $ = exports

var log = java.util.logging.Logger.getLogger("tengo-test")

var tengo = require("tengo")
var assert = require("assert")

var resetDb = function() {
	var keys = tengo.runQuery(tengo.query({kind:"person"}).setKeysOnly()).map(function(r) { return r.key })
	tengo.remove(keys)
}

$.testBasic = function() {
	resetDb()	
	var k1 = tengo.put({kind:"person", name: "Aure", age:33})
	var r1 = tengo.get(k1)
	assert.equal(new java.lang.String("Aure"), r1.name)
	assert.equal(new java.lang.Double(33.0), r1.age)	
	assert.equal("person", r1.key.kind)
	
	r1.name = "Aureliano"
	var k2 = tengo.put(r1)
	var r2 = tengo.get(k2)
	
	assert.javaEqual(k1, k2)
	assert.equal("Aureliano", r2.name)
}

$.testTransaction = function() {
	resetDb()	
	var result = null
	var key = null

	tengo.transaction(function(tr) {
		key = tr.put({kind:"person", name: "Aure", age:33})
	})
	tengo.transaction(function(tr) {
		result = tr.get(key)
	})
	
	assert.equal(new java.lang.String("Aure"), result.name)
	assert.equal(new java.lang.Double(33.0), result.age)
	assert.equal("person", result.key.kind)
}